package com.kcwl.carrier.infrastructure.repository.persostence;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kcwl.carrier.domain.entity.*;
import com.kcwl.carrier.domain.repository.ICarrierInfoRepository;
import com.kcwl.carrier.infrastructure.enums.UserLockEnum;
import com.kcwl.carrier.infrastructure.repository.mapper.CarrierInfoMapper;
import com.kcwl.carrier.infrastructure.repository.po.CarrierInfoPO;
import com.kcwl.carrier.interfaces.dto.CooperateEnterpriseDTO;
import com.kcwl.carrier.interfaces.query.CarrierListQuery;
import com.kcwl.ddd.application.constants.IdentityTypeEnum;
import com.kcwl.ddd.application.constants.YesNoEnum;
import com.kcwl.ddd.domain.entity.KcPage;
import com.kcwl.ddd.domain.repository.BaseRepositoryImpl;
import com.kcwl.framework.utils.CollectionUtil;
import com.kcwl.framework.utils.KcBeanConverter;
import com.kcwl.framework.utils.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 刘旭超
 * @date 2022年07月11日 16:33
 * @Description
 */
@Service
public class CarrierInfoRepositoryImpl extends BaseRepositoryImpl<CarrierInfoMapper, CarrierInfoPO>
    implements ICarrierInfoRepository {

    @Resource
    private CarrierInfoMapper carrierInfoMapper;

    @Override
    public CarrierInfo queryByUserId(Long userId) {
        //        2.0版本
        //        QueryWrapper<CarrierInfoPO> queryWrapper = new QueryWrapper<>();
        //        queryWrapper.eq("user_id", userId);

        return KcBeanConverter.toBean(carrierInfoMapper.selectById(userId), CarrierInfo.class);
    }

    @Override
    public CarrierInfo queryByCarrierId(Long carrierId) {
        return KcBeanConverter.toBean(carrierInfoMapper.selectById(carrierId), CarrierInfo.class);
    }

    @Override
    public int insert(CarrierInfo carrierInfo) {
        Date currentDate = new Date();
        carrierInfo.setCreateTime(currentDate);
        carrierInfo.setCreateUserId(carrierInfo.getCreateUserId());
        carrierInfo.setIsLock(UserLockEnum.UNLOCK.getCode());
        carrierInfo.setDeleted(YesNoEnum.NAY.getValue());
        return carrierInfoMapper.insert(KcBeanConverter.toBean(carrierInfo, CarrierInfoPO.class));
    }

    @Override
    public int updateById(CarrierInfo carrierInfo) {
        carrierInfo.setUpdateTime(new Date());
        return carrierInfoMapper.updateById(KcBeanConverter.toBean(carrierInfo, CarrierInfoPO.class));
    }

    @Override
    public KcPage<SingleCarrierListInfo> querySingleList(SingleCarrierInfoListCondition condition) {
        condition.getIdentityList().add(IdentityTypeEnum.CARRIER_DRIVER.getSubType());
        condition.getIdentityList().add(IdentityTypeEnum.CARRIER_SFLTOP.getSubType());
        condition.getIdentityList().add(IdentityTypeEnum.CARRIER_BRKR.getSubType());
        condition.getIdentityList().add(IdentityTypeEnum.CARRIER_TDSPR.getSubType());
        IPage<SingleCarrierListInfo> iPage =
            carrierInfoMapper.querySingleList(new Page<>(condition.getCurPagerNo(), condition.getPageSize()),
                condition);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public KcPage<FirmCarrierListInfo> queryFirmList(FirmCarrierInfoListCondition condition) {
        condition.getIdentityList().add(IdentityTypeEnum.CARRIER_TCOM_MANAGER.getSubType());
        IPage<FirmCarrierListInfo> iPage =
            carrierInfoMapper.queryFirmList(new Page<>(condition.getCurPagerNo(), condition.getPageSize()), condition);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public List<CarrierInfo> getByIds(List<Long> ids) {
        QueryWrapper<CarrierInfoPO> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        return KcBeanConverter.toList(carrierInfoMapper.selectList(wrapper), CarrierInfo.class);
    }

    @Override
    public List<CarrierInfo> getCarrierDrivers(Long userId, String keyWord) {
        return carrierInfoMapper.getCarrierDrivers(userId, keyWord);
    }

    @Override
    public KcPage<CarrierList> getCarrierList(CarrierListQuery query) {
        IPage<CarrierList> page =
            carrierInfoMapper.getCarrierList(new Page<CarrierListQuery>(query.getCurPagerNo(), query.getPageSize()),
                query);
        return new KcPage<>(page.getTotal(), page.getCurrent(), page.getSize(), page.getRecords());
    }

    @Override
    public KcPage<CooperateEnterpriseDTO> queryCooperateEnterpriseList(CooperateEnterpriseCondition condition) {
        IPage<CooperateEnterpriseDTO> iPage = carrierInfoMapper.queryCooperateEnterpriseList(
            new Page<>(condition.getCurPagerNo(), condition.getPageSize()), condition);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public CarrierInfo getCarrierInfo(CarrierInfo carrierInfo) {
        QueryWrapper<CarrierInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", carrierInfo.getId());
        return KcBeanConverter.toBean(carrierInfoMapper.selectOne(queryWrapper), CarrierInfo.class);
    }

    @Override
    public List<DriverDispatchInfo> queryDispatchDriverInfo(String mobile) {
        return carrierInfoMapper.queryDispatchDriverInfo(mobile);
    }

    @Override
    public List<DriverDispatchInfo> queryDispatchDriverAndVehicleInfo(String queryInfo) {
        return carrierInfoMapper.queryDispatchDriverAndVehicleInfo(queryInfo);
    }

    @Override
    public List<CompanyDispatchInfo> queryDispatchCompanyInfo(String mobile) {
        return carrierInfoMapper.queryDispatchCompanyInfo(mobile);
    }

    @Override
    public List<CompanyDispatchInfo> queryDispatchCompanyAndAuxiliaryInfo(String mobile) {
        return carrierInfoMapper.queryDispatchCompanyAndAuxiliaryInfo(mobile);
    }

    @Override
    public List<CompanyDispatchInfo> queryDispatchAuxiliaryInfo(String mobile) {
        return carrierInfoMapper.queryDispatchAuxiliaryInfo(mobile);
    }

    @Override
    public List<CompanyDispatchInfo> queryDispatchCarrierInfo(List<Long> ids) {
        return carrierInfoMapper.queryDispatchCarrierInfo(ids);
    }

    @Override
    public List<Long> queryDispatchDriverOrg(Long userId) {
        return carrierInfoMapper.queryDispatchDriverOrg(userId);
    }

    @Override
    public Class<CarrierInfoPO> getPoClass() {
        return CarrierInfoPO.class;
    }

    @Override
    public List<CarrierInfo> getCarrierListByParams(CarrierListQuery query) {
        QueryWrapper<CarrierInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!ObjectUtil.isEmpty(query.getCancer()), "cancer", query.getCancer());
        queryWrapper.eq(!ObjectUtil.isEmpty(query.getIdentityType()), "identity_type", query.getIdentityType());
        queryWrapper.like(!StringUtil.isEmpty(query.getDriverName()), "name", query.getDriverName());
        queryWrapper.like(!StringUtil.isEmpty(query.getMobile()), "mobile", query.getMobile());
        queryWrapper.eq(ObjectUtil.isNotEmpty(query.getAuthenticationStatus()), "authentication_status", query.getAuthenticationStatus());
        queryWrapper.in(!CollectionUtil.isEmpty(query.getMobileList()), "mobile", query.getMobileList());
        queryWrapper.in(!CollectionUtil.isEmpty(query.getUserIds()), "id", query.getUserIds());
        queryWrapper.in(!CollectionUtil.isEmpty(query.getNameList()), "name", query.getNameList());
        queryWrapper.and(!StringUtil.isEmpty(query.getContent()),a -> a.like("name", query.getContent()).or().like("mobile", query.getContent()));
        return KcBeanConverter.toList(carrierInfoMapper.selectList(queryWrapper), CarrierInfo.class);
    }

    @Override
    public KcPage<TmsDriverInfo> queryTmsDriverInfoPage(TmsDriverInfoCondition condition) {
        IPage<TmsDriverInfo> iPage =
            carrierInfoMapper.queryTmsDriverInfoPage(new Page<>(condition.getCurPagerNo(), condition.getPageSize()),
                condition);
        return new KcPage<>(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public List<CarrierInfo> queryByMobile(List<String> mobileList) {
        QueryWrapper<CarrierInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("mobile", mobileList);
        queryWrapper.eq("deleted", YesNoEnum.NAY.getValue());
        return KcBeanConverter.toList(carrierInfoMapper.selectList(queryWrapper), CarrierInfo.class);
    }

    @Override
    public List<CarrierInfo> queryByName(List<String> nameList) {
        QueryWrapper<CarrierInfoPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("name", nameList);
        queryWrapper.eq("deleted", YesNoEnum.NAY.getValue());
        return KcBeanConverter.toList(carrierInfoMapper.selectList(queryWrapper), CarrierInfo.class);
    }
}
